﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using CommunitySystem.SystemService;

namespace CommunitySystem
{
    public partial class DataDictionaryControl : UserControl
    {
        SystemService.SystemServiceClient client = new SystemService.SystemServiceClient();
        DataDictionaryWindow dw = new DataDictionaryWindow();
        public DataDictionaryControl()
        {
            InitializeComponent();
            



            this.Loaded += new RoutedEventHandler(DataDictionaryControl_Loaded);
        }



        /// <summary>
        /// 加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void DataDictionaryControl_Loaded(object sender, RoutedEventArgs e)
        {
            LoadData();
        }

        /// <summary>
        /// 
        /// </summary>
        private void LoadData()
        {
            client.GetDictionarysCompleted += new EventHandler<GetDictionarysCompletedEventArgs>(client_GetDictionarysCompleted);
            client.GetDictionarysAsync();
            if (DataDictionary.SelectedItem != null)
            {
                var PID = DataDictionary.SelectedItem as DataDictionaryModel;
                if(PID != null)
                {
                    client.GetDataDictionaryItemCompleted += new EventHandler<GetDataDictionaryItemCompletedEventArgs>(client_GetDataDictionaryItemCompleted);
                    client.GetDataDictionaryItemAsync(PID.Id);
                }
                
            }
        }
        /// <summary>
        /// 绑定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void client_GetDictionarysCompleted(object sender, GetDictionarysCompletedEventArgs e)
        {
            //throw new NotImplementedException();
            DataDictionary.ItemsSource = e.Result;
            //Worker.IsBusy = false;
        }

       

        private void NewData_Click(object sender, RoutedEventArgs e)
        {
            dw = new DataDictionaryWindow();
            dw.Show();


            //给弹出窗口关闭注册事件
            dw.Closed += new EventHandler(dw_Closed);
        }

        void dw_Closed(object sender, EventArgs e)
        {
            //throw new NotImplementedException();
            LoadData();
        }

        /// <summary>
        /// 绑定子项目
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DataDictionary_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            //Worker.IsBusy = true;


             
            DataGrid grid = sender as DataGrid; 
            if (grid.SelectedItems.Count > 0) 
            { 
                DataDictionaryModel DDModel = (DataDictionaryModel)grid.SelectedItems[0];//获取当前选中的行数据并转换为对应的实体 


                client.GetDataDictionaryItemCompleted += new EventHandler<GetDataDictionaryItemCompletedEventArgs>(client_GetDataDictionaryItemCompleted);
                client.GetDataDictionaryItemAsync(DDModel.Id);
                //MessageBox.Show(DDModel.Id.ToString());
            } 
              
        }
        /// <summary>
        /// 绑定事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void client_GetDataDictionaryItemCompleted(object sender, GetDataDictionaryItemCompletedEventArgs e)
        {
            //throw new NotImplementedException();
            DataDictionaryItem.ItemsSource = null;
            DataDictionaryItem.ItemsSource = e.Result;
           // Worker.IsBusy = false;
        }

        /// <summary>
        /// 编辑数据字典
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void EditData_Click(object sender, RoutedEventArgs e)
        {
            if (DataDictionary.SelectedItems.Count > 0)
            {
                DataDictionaryModel DDModel = (DataDictionaryModel)DataDictionary.SelectedItems[0];
                dw = new DataDictionaryWindow(DDModel.Id);
                dw.Show();
            }

            //给弹出窗口关闭注册事件
            dw.Closed += new EventHandler(dw_Closed);
        }
        /// <summary>
        /// 删除数据字典 （父子）
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DeleteData_Click(object sender, RoutedEventArgs e)
        {
            //Worker.IsBusy = true;
            if (MessageBox.Show("确定删除吗？", "提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
            {
                if (DataDictionary.SelectedItems.Count > 0)
                {
                    DataDictionaryModel DDModel = (DataDictionaryModel)DataDictionary.SelectedItems[0];
                    client.DeleteDataDictionaryCompleted -= new EventHandler<DeleteDataDictionaryCompletedEventArgs>(client_DeleteDataDictionaryCompleted);
                    client.DeleteDataDictionaryCompleted += new EventHandler<DeleteDataDictionaryCompletedEventArgs>(client_DeleteDataDictionaryCompleted);
                    client.DeleteDataDictionaryAsync(DDModel.Id);
                }
            }
            
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void client_DeleteDataDictionaryCompleted(object sender, DeleteDataDictionaryCompletedEventArgs e)
        {
            //throw new NotImplementedException();
            SilverlightMessageBoxLibrary.Message.InfoMessage("删除成功！");
            LoadData();
        } 
       
    }
}
